package cn.school.service;

import java.util.Date;
import java.util.List;

import cn.school.entity.Page;
import cn.school.entity.Page2;
import org.apache.ibatis.session.SqlSession;

import cn.school.dao.StudentDao;
import cn.school.entity.Student;
import cn.school.util.MyBatisUtil;

public class StudentService {
	public static final int PAGE_SIZE = 20;

	public Student get(long stuno){
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.get(stuno);
		} finally {
			MyBatisUtil.close(s);
		}
	}
	public Page<Student> search(int pageNo, int pageSize, String name, String tele, Date enterDateStart,
								 Date enterEnd) {
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			List<Student> list =studentDao.search(pageNo, pageSize, name, tele, enterDateStart, enterEnd);
			int total=studentDao.searchTotal(name, tele, enterDateStart, enterEnd);

			return new Page<Student>(total,list,pageSize);
		} finally {
			MyBatisUtil.close(s);
		}
	}
/*	public List<Student> search(int pageNo, int pageSize, String name, String tele, Date enterDateStart,
			Date enterEnd) {
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.search(pageNo, pageSize, name, tele, enterDateStart, enterEnd);
		} finally {
			MyBatisUtil.close(s);
		}
	}*/

	public int searchTotal( String name,  String tele,
						    Date enterDateStart, Date enterEnd){
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.searchTotal(name, tele, enterDateStart, enterEnd);
		} finally {
			MyBatisUtil.close(s);
		}
	}
//	public Student login( long stuno, String p){
//		SqlSession s = MyBatisUtil.get();
//		try {
//			StudentDao studentDao = s.getMapper(StudentDao.class);
//			return studentDao.login(stuno,p);
//		} finally {
//			MyBatisUtil.close(s);
//		}
//	}
}
